(function() {
    angular.module('BlurAdmin.pages.SystemSettings')
        .controller('PatternCtrl', PatternCtrl);

    function PatternCtrl($scope, $openModal, $http, $myconfig, $mydialog) {
        //弹窗框
        $scope.open = $openModal; //创建实例化对象
        function fn(val) { //关闭模态框方法
            $scope.myopen.opt_close = val;
        }
        $scope.myopen = function(page, size) { //打开模态框
            $scope.open.newOpen(page, size, $scope, fn);
        }
        $scope.myopen2 = function(page, size) { //打开模态框
        opt={};
            $scope.open.newOpen(page, size, $scope, fn, 'static');
        }

        $scope.a = {
            a1: true,
            a2: true,
            a3: true,
            a4: true,
        };
        $scope.am = {};
        $scope.bianji_fn = function(val) {
            val = (val == true) ? false : true;
        }

        //获取设备列表
        $http.get($myconfig.api.app_list_device_b).then(function(res) {
            $scope.list_data = res.data.data;
        });
        //点击编辑设备
        $scope.patterm_data = {

        }

        var opt = {};

        $scope.index_num = null;
        $scope.type_num = null;
        $scope.time = {};
        $scope.bianji = function(opt, index, type) {
            var o = JSON.stringify(opt);
            $scope.index_num = index;
            $scope.type_num = type;
            $scope.patterm_data = JSON.parse(o); //用于绑定数据
            $scope.patterm_data.speed = parseInt($scope.patterm_data.speed);
            $scope.patterm_data.redscale = parseInt($scope.patterm_data.redscale);
            $scope.patterm_data.bluescale = parseInt($scope.patterm_data.bluescale);
            $scope.patterm_data.redlight = parseInt($scope.patterm_data.redlight);
            $scope.patterm_data.bluelight = parseInt($scope.patterm_data.bluelight);
            $scope.patterm_data.strength = parseInt($scope.patterm_data.strength);

            console.log(opt);
            var a = JSON.parse(o);
            $scope.time.t = parseInt(a.durations / 60);
            $scope.myopen2('app/pages/SystemSettings/Pattern/newPattrn/EditPattwm.html', 'lg');

        }

        // 获取设置的数字
        $scope.dianji = function(e, type) {
            // console.log($(e.delegateTarget).find('.irs-single').text());
            // var val = $(e.delegateTarget).find('.irs-single').text();
            // type == 'speed' && (opt.speed = val);
            // type == 'redscale' && (opt.redscale = val);
            // type == 'bluescale' && (opt.bluescale = val);
            // type == 'redlight' && (opt.redlight = val);
            // type == 'bluelight' && (opt.bluelight = val);
        };

        $scope.Submit = function() {
            jiance();
        }

        //检测是否设置名称，转化时间单位
        function jiance() {
            if (!$scope.patterm_data.name) {
                return $mydialog.fail('请设置模式名称')
            }

            //获取设置的数值
            opt.speed=$("#speed span .irs .irs-single").html();
            opt.redscale=$("#redscale span .irs .irs-single").html();
            opt.bluescale=$("#bluescale span .irs .irs-single").html();
            opt.bluelight=$("#redlight span .irs .irs-single").html();
            opt.redlight=$("#redlight span .irs .irs-single").html();
            
            
            
            opt.speed && ($scope.patterm_data.speed = parseInt(opt.speed));
            opt.redscale && ($scope.patterm_data.redscale = parseInt(opt.redscale));
            opt.bluescale && ($scope.patterm_data.bluescale = parseInt(opt.bluescale));
            opt.redlight && ($scope.patterm_data.redlight = parseInt(opt.redlight));
            opt.bluelight && ($scope.patterm_data.bluelight = parseInt(opt.bluelight));
            var optss = $scope.list_data[$scope.type_num];
            // opt.durations = $scope.time.t * 60;

            optss.mode[$scope.index_num] = $scope.patterm_data;
            optss.mode[$scope.index_num].durations = $scope.time.t * 60;
            optss['device_id'] = optss.id;


            delete optss.id;
            $http.post($myconfig.api.app_update_device, optss).then(function(res) {
                if (res.data.success) {
            console.log(opt);
                    $mydialog.success('设置成功');
                    $scope.myopen.opt_close();
                    $http.get($myconfig.api.app_list_device_b).then(function(res) {
                        $scope.list_data = res.data.data;
                    });
                } else {
                    $mydialog.fail(res.data.data)
                }
            });

        }


        $scope.shijian = [{
            num: 1,
            text: '1分钟'
        }, {
            num: 5,
            text: '5分钟'
        }, {
            num: 10,
            text: '10分钟'
        }, {
            num: 15,
            text: '15分钟'
        }, {
            num: 20,
            text: '20分钟'
        }, {
            num: 25,
            text: '25分钟'
        }, {
            num: 30,
            text: '30分钟'
        }, {
            num: 35,
            text: '35分钟'
        }, {
            num: 40,
            text: '40分钟'
        }, {
            num: 45,
            text: '45分钟'
        }, {
            num: 50,
            text: '50分钟'
        }, {
            num: 55,
            text: '55分钟'
        }, {
            num: 60,
            text: '60分钟'
        }];




    }
})();